﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef struct StuNode
{
	long long  ID;
	double scores;

}SN;
typedef struct StuList
{
	SN* ps;
	int size;
}SL;

void menu()
{
	printf("1.录入每个学生的学号和成绩\n");
	printf("2.计算课程的总分和平均分\n");
	printf("3.按成绩由高到低排出名次表\n");
	printf("4.按学号由小到大排出成绩表\n");
	printf("5.按学号查询学生排名及其考试成绩\n");
	printf("6.对学生成绩进行分类，并统计每个类别学生所占的百分比\n");
	printf("7.输出每个学生的学号，考试成绩，课程总分和平均分\n");
	printf("0.退出\n");
}
void IintSL(SL*s,int n)
{
	 s->ps = (SN*)malloc(sizeof(SN) * n);
	if (s->ps == NULL)
	{
		perror("malloc file");
	}
	s->size = n;
}

void InputScores(SL*s,int n)
{
	printf("请输入学号和成绩: \n");
	for (int i = 0; i < n; i++)
	{
		scanf("%lld %lf", &s->ps[i].ID, &s->ps[i].scores);
	}
}
void ALlScores(SL* s, int n)
{
	double sum = 0;
	double average = 0;
	for (int i = 0; i < n; i++)
	{
		sum += s->ps[i].scores;
	}
	average = sum / n;
	printf("总分是 %.2lf,平均分是 %.2lf\n", sum, average);

}
void ScoresSort(SL* s, int n)
{
	//冒泡排序从高到低
	for (int i = 0; i < n-1; i++)
	{
		for (int j = 0; j < n-1-i; j++)
		{
			if (s->ps[j].scores < s->ps[j + 1].scores)
			{
				SN tem = s->ps[j];
				s->ps[j]= s->ps[j + 1];
				s->ps[j + 1] = tem;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		printf("%lld %.2lf\n", s->ps[i].ID, s->ps[i].scores);
	}

}
void IDSort(SL*s, int n)
{
	//冒泡排序从低到高
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - 1 - i; j++)
		{
			if (s->ps[j].ID > s->ps[j + 1].ID)
			{
				SN tem = s->ps[j];
				s->ps[j] = s->ps[j + 1];
				s->ps[j + 1] = tem;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		printf("%lld %.2lf\n", s->ps[i].ID, s->ps[i].scores);
	}

}

void Inquire(SL* s, int n)
{
	//先进行成绩排序
	ScoresSort(s, n);

	long long _ID;
	printf("请输入要查询的学号: ");
	scanf("%lld", &_ID);
	int count = 0;//计数器
	for (int i = 0; i < n; i++)
	{
		count++;
		if (s->ps[i].ID == _ID)
		{
			printf("该学生的排名是 NO.%d ,考试成绩是 %.2lf\n", count, s->ps[i].scores);
		}
	}
}

void ScoresAnalysis(SL* s, int n)
{
	double a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0;
	for (int i = 0; i < n; i++)
	{
		double arr = s->ps[i].scores;
		if (arr >= 90 && arr <= 100)
		{
			a1 ++;
		}
		if (arr >= 80 && arr <= 89)
		{
			a2++;
		}
		if (arr >= 70 && arr <= 79)
		{
			a3++;
		}
		if (arr >= 60 && arr <= 69)
		{
			a4++;
		}
		if (arr >= 0 && arr <= 59)
		{
			a5++;
		}
	}
	double sum1 = a1 / n;
	printf("优秀的人数 %.0lf \n", a1);
	printf("优秀人数占的百分比：%.2lf\n", sum1);
	double sum2 = a2 / n;
	printf("良好的人数 %.0lf \n", a2);
	printf("良好人数占的百分比：%.2lf\n", sum2);
	double sum3 = a3 / n;
	printf("中等的人数 %.0lf \n", a3);
	printf("中等人数占的百分比：%.2lf\n", sum3);
	double sum4 = a4 / n;
	printf("及格的人数 %.0lf \n", a4);
	printf("及格人数占的百分比：%.2lf\n", sum4);
	double sum5 = a5 / n;
	printf("不及格的人数 %.0lf \n", a5);
	printf("不及格人数占的百分比：%.2lf\n", sum5);

}
void OutPut(SL* s, int n)
{
	printf("学生学号和成绩是： \n");
	for (int i = 0; i < n; i++)
	{
		printf("%lld %.2lf\n", s->ps[i].ID, s->ps[i].scores);
	}
	ALlScores(s, n);

}

int main()
{
	int n = 0;
	printf("请输入学生人数: ");
	scanf("%d", &n);
	SL s;
	//初始化链表
	IintSL(&s,n);


	int k = 0;
	do
	{
		menu();
		printf("请输入对应的指令: ");
		scanf("%d",&k);
		switch (k)
		{
		case 1:
			InputScores(&s,n);
			break;

		case 2:
			ALlScores(&s, n);
			break;

		case 3:
			ScoresSort(&s, n);
			break;

		case 4:
			IDSort(&s, n);
			break;

		case 5:
			Inquire(&s, n);
			break;

		case 6:
			ScoresAnalysis(&s, n);
			break;
		case 7:
			OutPut(&s, n);
			break;

		case 0:
			printf("退出学生成绩管理系统\n");
			break;
		default:
			printf("输入错误，请重新输入\n");
			break;

		}
	} while (k);
	

	return 0;
}


//999 90
//888 80
//666 60
//777 70
//222 20